package com.gable.report.controller;

import com.gable.report.common.util.ReturnResultUtil;
import com.gable.report.model.ChartsHospitalQuery;
import com.gable.report.model.User;
import com.gable.report.service.ChartsForHospitalMaintainerService;
import com.gable.report.util.ChartsUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * Created by lvdouStart on 2017/7/27.
 * 医院报表科室维修商
 */
@Controller
@RequestMapping("charts/hospital/department-maintainer")
public class ChartsForHospitalDepartmentMaintainerController {

    private final Log logger = LogFactory.getLog(this.getClass());

    @Resource
    HttpServletRequest request;

    @Autowired
    ChartsForHospitalMaintainerService chartsForHospitalMaintainerService;

    @RequestMapping("/page")
    public String page(ChartsHospitalQuery chartsHospitalQuery){
        User user = (User)request.getSession().getAttribute("loginUser");
        return ChartsUtil.checkAuthority(user,chartsHospitalQuery) ? "charts/hospital/department-maintainer" : "charts-authority";
    }

    /** 
     * @Author: 杨青桦
     * @Description: 维修商概览框
     * @Date: 2017/8/2 11:51
     * @param chartsHospitalQuery
     * @return
     */
    @RequestMapping(value="/overview",method= RequestMethod.POST,produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryAllAssets(ChartsHospitalQuery chartsHospitalQuery){
        User user = (User)request.getSession().getAttribute("loginUser");
        Long hospitalId = ChartsUtil.getHospitalId(user,chartsHospitalQuery);
        Map<String,Object> map;
        try {
            map = chartsForHospitalMaintainerService.queryAllAssetsAndRepairSizes(hospitalId, chartsHospitalQuery);
        }catch (Exception e){
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
        return new ReturnResultUtil(1,"查询成功",map);
    }

    /** 
     * @Author: 杨青桦
     * @Description:  科室
     * @Date: 2017/7/27 15:34
     * @param chartsHospitalQuery
     * @return
     */
    @RequestMapping(value="/department",method= RequestMethod.POST,produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryDepartments(ChartsHospitalQuery chartsHospitalQuery){
        User user = (User)request.getSession().getAttribute("loginUser");
        Long hospitalId = ChartsUtil.getHospitalId(user,chartsHospitalQuery);
        Map<String,Object> map;
        try {
            map = chartsForHospitalMaintainerService.queryDepartments(hospitalId, chartsHospitalQuery);
        }catch (Exception e){
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
        return new ReturnResultUtil(1,"查询成功",map);
    }


    /**
     * @Author: 杨青桦
     * @Description:  设备型号
     * @Date: 2017/7/27 15:33
     * @param chartsHospitalQuery
     * @return
     */
    @RequestMapping(value="/type",method= RequestMethod.POST,produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryDeviceTypes(ChartsHospitalQuery chartsHospitalQuery){
        User user = (User)request.getSession().getAttribute("loginUser");
        Long hospitalId = ChartsUtil.getHospitalId(user,chartsHospitalQuery);
        Map<String,Object> map;
        try {
            map = chartsForHospitalMaintainerService.queryDeviceTypes(hospitalId, chartsHospitalQuery);
        }catch (Exception e){
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
        return new ReturnResultUtil(1,"查询成功",map);
    }

    /** 
     * @Author: 杨青桦
     * @Description:  设备型号
     * @Date: 2017/7/27 15:33
     * @param chartsHospitalQuery
     * @return
     */
    @RequestMapping(value="/model",method= RequestMethod.POST,produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryDeviceModels(ChartsHospitalQuery chartsHospitalQuery){
        User user = (User)request.getSession().getAttribute("loginUser");
        Long hospitalId = ChartsUtil.getHospitalId(user,chartsHospitalQuery);
        Map<String,Object> map;
        try {
            map = chartsForHospitalMaintainerService.queryDeviceModels(hospitalId, chartsHospitalQuery);
        }catch (Exception e){
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
        return new ReturnResultUtil(1,"查询成功",map);
    }


    /**
     * @Author: 杨青桦
     * @Description: 月份数据
     * @Date: 2017/7/20 11:42
     * @param chartsHospitalQuery
     * @return
     */
    @RequestMapping(value="/month-trend",method= RequestMethod.POST,produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryForMonth(ChartsHospitalQuery chartsHospitalQuery){
        User user = (User)request.getSession().getAttribute("loginUser");
        Long hospitalId = ChartsUtil.getHospitalId(user,chartsHospitalQuery);
        Map<String,Object> map;
        try {
            map = chartsForHospitalMaintainerService.queryForMonth(hospitalId, chartsHospitalQuery);
        }catch (Exception e){
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
        return new ReturnResultUtil(1,"查询成功",map);
    }


    /**
     * @Author: 杨青桦
     * @Description:  根据设备型号查询具体设备
     * @Date: 2017/8/2 13:40
     * @param chartsHospitalQuery
     * @return
     */
    @RequestMapping(value="/device",method= RequestMethod.POST,produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryDeviceInfoByModelName(ChartsHospitalQuery chartsHospitalQuery){
        User user = (User)request.getSession().getAttribute("loginUser");
        Long hospitalId = ChartsUtil.getHospitalId(user,chartsHospitalQuery);
        Map<String,Object> map;
        try {
            map = chartsForHospitalMaintainerService.queryDeviceInfoByModelName(hospitalId, chartsHospitalQuery);
        }catch (Exception e){
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
        return new ReturnResultUtil(1,"查询成功",map);
    }


}
